package net.adetech.cjg.innerservice.controller;

import com.alibaba.fastjson.JSONObject;
import net.adetech.cjg.innerservice.entity.Zs;
import net.adetech.cjg.innerservice.mapper.ZsDao;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class ZsController {


    @Autowired
    ZsDao zsDao;



    @RequestMapping(value = "/cjg/hqyzm",produces = "application/json")
    public String hqyzm(String ip,String zhlx,String url)
    {
        Zs zs = new Zs();
        zs.setIp(ip);
        zs.setZhlx(zhlx);
        zs.setDldz(url);
        zs.setZt("2");
        this.zsDao.saveUrl(zs);

        JSONObject resultJson = new JSONObject();

        for (int i = 0; i < 60; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            zs = this.zsDao.getZsByIp(ip);
            if ("3".equals(zs.getZt()))
            {

                resultJson.put("result","ok");
                resultJson.put("yzm",zs.getYzm());
                return resultJson.toJSONString();
            }

        }
        resultJson.put("result","error");
        resultJson.put("msg","获取超时");
        return resultJson.toJSONString();
    }

//    public String hqyzm(String ip,String zhlx,String url)
//    {
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        Zs zs = new Zs();
//        zs.setIp(ip);
//        zs.setZhlx(zhlx);
//        zs.setDldz(url);
//        zs.setZt("3");
//        this.zsDao.saveUrl(zs);
//
//
//        JSONObject resultJson = new JSONObject();
//
//
//        resultJson.put("result","ok");
//        resultJson.put("yzm","iVBORw0KGgoAAAANSUhEUgAAAFAAAAAeCAYAAAC7Q5mxAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAXSSURBVGhD1ZgHTuRAEEVNzhlxArj/eZBAQiCQyDkH77zaed7CDMssjGH2S622O1b9/tXddtFCOTc3V5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TD/Vk++YlHtgSS7PJE5hL/n4Ygh/B1rzF68UxzMOuXqZIByiHmLp52mb90egEzpoGWOxGAAyJQRIFsrNe5hkqXjmdD+0LwsHUCF2q8Zv2QO5VrB6EKOxPOMsREnu1tZW5MBQNgyFyrGPTuY2AMcYnzEITxdrc3Mz7oaQLaFgd3c3xjCU6WsfwLNzZtS3jyYQIazKMKRuGDD0IBTnXWGcyYeBBEKMBBCimUD6emWh3DqdJeSBczg3F3UUy3weStTZThANzK+Km0bL/qKaLK/i+vp65aBOuicaGrne0Mw/HHAwt5HU/NXCWMyL6oBlLJ5lwPYo1HEkz4XO9rMgnVTZa1S/9AGG5dDBUS7FGC1pgBBGtRwS7k0ABWZFSDhXHX8Y5CQkYG9vL+ZThZYzjvMwFuXZHkE585Pbt2kU7GMoRWeBIa2jGiY5rKz7H4nnHDJZhYwF4baFiHz3NETtj+pol9+Zl9DNd0vAPKoUmyRN+79FgRrkHuSkGJdv/wCnrKcc9aJS9z4dkMBOKqnDz8OsGsb2GbhwlJNYgH5BwcnGKuariMZTxiGj4YAQgjjL6ycv+My/OBTpvFlBkrexsRHzuqj52/kn0bLlz8VUY4GEZmJoC3GGr/06XRcYSyL+BraOrDbHzGTWVU8os6dm234KQaDgJNUBDhOcgAhVBgxPDxvC2v0Th7Iy/sVB90Kwvb3dfvoNFgg7WTjmdWG6WaCmEYeIRAHyrIisSgjJRuc9jjo3dBbBhegWzlPfiyGWZ+odH+RD7ycR1xhIycapuKw29zWckWCcsk19D5WAjwAReW4/y+oE+Y5tPjv3z6EsfwHSwYbbOYtAAQAAAABJRU5ErkJggg==");
//        return resultJson.toJSONString();
//    }





    @RequestMapping(value = "/cjg/zsdl",produces = "application/json")
    public String zsdl(String yzm,String ip)
    {
        Zs zs = new Zs();
        zs.setIp(ip);
        zs.setYzm(yzm);
        zs.setZt("4");
        this.zsDao.saveYzm(zs);
        JSONObject resultJson = new JSONObject();

        for (int i = 0; i < 60; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            zs = this.zsDao.getZsByIp(ip);
            if (!"4".equals(zs.getZt()))
            {
                if ("1".equals(zs.getZt()))
                {
                    resultJson.put("result","ok");
                    resultJson.put("msg","登录成功");
                }
                else {
                    resultJson.put("result", "error");
                    resultJson.put("msg", "登录失败");
                }
                return resultJson.toJSONString();
            }

        }
        resultJson.put("result","timeout");
        resultJson.put("msg", "登录超时");
        return resultJson.toJSONString();
    }

//    public String zsdl(String yzm,String ip)
//    {
//
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        JSONObject resultJson = new JSONObject();
//        if ("3U7P".equalsIgnoreCase(yzm))
//        {
//            Zs zs = new Zs();
//            zs.setIp(ip);
//            zs.setYzm(yzm);
//            zs.setZt("1");
//            resultJson.put("result","ok");
//            resultJson.put("msg","登录成功");
//        }
//        else {
//            Zs zs = new Zs();
//            zs.setIp(ip);
//            zs.setYzm(yzm);
//            zs.setZt("0");
//            resultJson.put("result", "error");
//            resultJson.put("msg", "登录失败");
//        }
//        return resultJson.toJSONString();
//    }





    @RequestMapping(value = "/cjg/hqzszt",produces = "application/json")
    public String hqzszt(boolean loginFlag)
    {
        String ip = this.getClientIp();
        JSONObject resultJson = new JSONObject();
        Zs zs = this.zsDao.getZsByIp(ip);
        if (zs!=null)
        {



            resultJson = JSONObject.parseObject(JSONObject.toJSONString(zs));
            if (loginFlag)
            {
                zs.setZt("1");
            }
            else {
                if ("1".equals(zs.getZt()))
                {
                    zs.setZt("0");
                }
            }
            this.zsDao.saveZt(zs);

            resultJson.put("result","ok");
        }
        else {
            resultJson.put("result","error");
        }
        return resultJson.toJSONString();
    }


    @RequestMapping(value = "/cjg/scyzm",produces = "application/json")
    public String scyzm(String yzm)
    {
        String ip = this.getClientIp();

        Zs zs = new Zs();
        zs.setIp(ip);
        zs.setYzm(yzm);
        zs.setZt("3");
        this.zsDao.saveYzm(zs);
        return "{result:'ok'}";
    }







    @Autowired
    private HttpServletRequest request;
    private String getClientIp() {
        String remoteAddr = "";

        if (request != null) {
            remoteAddr = request.getHeader("X-FORWARDED-FOR");
            if (remoteAddr == null || "".equals(remoteAddr)) {
                remoteAddr = request.getRemoteAddr();
            }
        }

        return remoteAddr;
    }

}
